package com.rex.saas.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rex.saas.bean.BaseResponse;
import com.rex.saas.bean.ResultCode;
import com.rex.saas.bean.request.DeviceListRequest;
import com.rex.saas.bean.response.DeviceResponse;
import com.rex.saas.bean.response.IndexCntNumResponse;
import com.rex.saas.constants.Constants;
import com.rex.saas.db.entity.SaasTslModel;
import com.rex.saas.service.DeviceService;
import com.rex.saas.service.TslModelService;
import com.rex.saas.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/index")
public class IndexController extends BaseController{
    @Autowired
    private TslModelService tslModelService;

    @Autowired
    private DeviceService deviceService;

    @RequestMapping("/airCon")
    public BaseResponse airCon(HttpServletRequest  request, @RequestParam Long spaceId) {
        log.info("airCon ===> spaceId:{}", spaceId);
        String token = request.getHeader("token");
        BaseResponse validateSpace = validateSpace(spaceId, token);
        if(BaseResponse.ok().getCode()!=validateSpace.getCode()){
            log.error("airCon validateSpace fail, validateSpace:{}", validateSpace);
            return validateSpace;
        }
        String tenantId = JwtUtils.getTenantId( token);
        List<SaasTslModel> tslModelList = tslModelService.queryTslModelByCategoryName(Constants.AIR_CON, tenantId);
        if (CollectionUtils.isEmpty(tslModelList)){
            return BaseResponse.error("产品模型不存在", ResultCode.PARAM_ERROR);
        }

        return BaseResponse.ok(getIndexCntNumResponse(tslModelList, spaceId, token));
    }

    private IndexCntNumResponse getIndexCntNumResponse(List<SaasTslModel> tslModelList, Long spaceId, String token) {
        DeviceListRequest deviceListRequest = new DeviceListRequest();
        deviceListRequest.setSpaceInfoId(spaceId);
        deviceListRequest.setProductKeys(tslModelList.stream().map(SaasTslModel::getProductKey).collect(Collectors.toList()));
        deviceListRequest.setPageNo(1);
        deviceListRequest.setPageSize(10000);
        Page<DeviceResponse> page = deviceService.listV2(deviceListRequest, token);
        List<DeviceResponse> deviceResponseList = page.getRecords();
        AtomicInteger online = new AtomicInteger();
        AtomicInteger offline = new AtomicInteger();
        deviceResponseList.forEach(deviceResponse -> {
            if (deviceResponse.getStatus() == 1) {
                online.getAndIncrement();
            } else {
                offline.getAndIncrement();
            }
        });
        return new IndexCntNumResponse(deviceResponseList.size(), online.get(), offline.get(),
                deviceResponseList.size() - online.get() - offline.get());
    }

    @RequestMapping("/light")
    public BaseResponse light(HttpServletRequest  request, @RequestParam Long spaceId) {
        log.info("light ===> spaceId:{}", spaceId);
        String token = request.getHeader("token");
        BaseResponse validateSpace = validateSpace(spaceId, token);
        if(BaseResponse.ok().getCode()!=validateSpace.getCode()){
            log.error("light validateSpace fail, validateSpace:{}", validateSpace);
            return validateSpace;
        }
        String tenantId = JwtUtils.getTenantId( token);
        List<SaasTslModel> tslModelList = tslModelService.queryTslModelByCategoryName(Constants.LIGHT, tenantId);
        if (CollectionUtils.isEmpty(tslModelList)){
            return BaseResponse.error("产品模型不存在", ResultCode.PARAM_ERROR);
        }
        return BaseResponse.ok(getIndexCntNumResponse(tslModelList, spaceId, token));
    }



}
